package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.dto.DeptDto;
import com.bjsxt.vo.DataGridView;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.Dept;
import com.bjsxt.mapper.DeptMapper;
import com.bjsxt.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public DataGridView listPage(DeptDto deptDto) {
        Page<Dept> page = new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        // 根据部门名称模糊查询
        qw.like(StringUtils.isNotBlank(deptDto.getDeptName()), Dept.COL_DEPT_NAME, deptDto.getDeptName());
        // 根据部门状态进行查询
        qw.eq(StringUtils.isNotBlank(deptDto.getStatus()), Dept.COL_STATUS, deptDto.getStatus());
        // 根据部门创建时间查询
        qw.ge(null != deptDto.getBeginTime(), Dept.COL_CREATE_TIME, deptDto.getBeginTime());
        qw.le(null != deptDto.getEndTime(), Dept.COL_CREATE_TIME, deptDto.getEndTime());
        qw.orderByAsc(Dept.COL_ORDER_NUM);
        deptMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    // 查询有效的部门
    @Override
    public List<Dept> list() {
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq(Dept.COL_STATUS, Constants.STATUS_TRUE);
        qw.orderByAsc(Dept.COL_ORDER_NUM);
        return deptMapper.selectList(qw);
    }

    //根据id查询
    @Override
    public Dept getOne(Long deptId) {
        return deptMapper.selectById(deptId);
    }

    @Override
    public int addDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        dept.setCreateBy(deptDto.getSimpleUser().getUserName());
        dept.setCreateTime(DateUtil.date());
        return deptMapper.insert(dept);
    }

    @Override
    public int updateDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
        return deptMapper.updateById(dept);
    }

    @Override
    public int deleteDeptByIds(Long[] deptIds) {
        List<Long> ids = Arrays.asList(deptIds);
        if (null!=ids&&ids.size()>0){
            return deptMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    /**
     * 根据科室id集合查询科室信息
     * @param deptIds
     * @return
     */
    @Override
    public List<Dept> listDeptByDeptIds(List<Long> deptIds) {
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.in(null!=deptIds,Dept.COL_DEPT_ID,deptIds);
        return deptMapper.selectList(qw);
    }

    /**
     * 修改指定科室的挂号编号
     * @param deptId
     * @param i
     */
    @Override
    public void updateDeptRegNumber(Integer deptId, int i) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setRegNumber(i);
        deptMapper.updateById(dept);
    }


}
